'use strict'
const Controller = require('egg').Controller;

class LoginController extends Controller{
    async login(){
        // console.log(this.ctx.request);
        const username = this.ctx.request.body.username;
        const password = this.ctx.request.body.password;
        const rid = this.ctx.request.body.restaurantId;
        const loginservice = this.ctx.service.loginservice;

        const result = await loginservice.getUser(username,password,rid);
        if(result){
            this.ctx.code = 200;
            this.ctx.body={
                user_id:result.uid,
                restaurantId:result.rid,
                token:this.config.token_seed
            };
        }else{
            this.ctx.code = 400;
        }
    }

    async logout(){
        this.ctx.code = 200;
        this.ctx.body='success';
    }

    async getRestaurant(){
        const param = this.ctx.request.body;
        const result = await this.ctx.service.loginservice.getRestaurant(param.user);

        this.ctx.code = 200;
        this.ctx.body = result;
    }
}

module.exports = LoginController;
